package com.yhy.wxzn.wms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhy.wxzn.common.enums.WmsMsgTypeEnums;
import com.yhy.wxzn.common.exception.WmsBusinessException;
import com.yhy.wxzn.wcs.service.IWmsAsyncMsgService;
import com.yhy.wxzn.wms.entity.WmsAsn;
import com.yhy.wxzn.wms.entity.WmsAsnDetail;
import com.yhy.wxzn.wms.mapper.WmsPdaMapper;
import com.yhy.wxzn.wms.service.IWmsAsnDetailService;
import com.yhy.wxzn.wms.service.IWmsAsnService;
import com.yhy.wxzn.wms.service.IWmsPdaService;
import com.yhy.wxzn.wms.vo.WmsASNReceiveVo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.entity.JeecgEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @Auther: 97354
 * @Date: 2023/11/3 13:53
 * @Description: WmsPdaServiceImpl
 * @Version 1.0.0
 */
@Service
@Slf4j
public class WmsPdaServiceImpl extends ServiceImpl<WmsPdaMapper, JeecgEntity> implements IWmsPdaService {

    @Autowired
    private WmsPdaMapper wmsPdaMapper;
    @Autowired
    private IWmsAsnDetailService asnDetailService;
    @Autowired
    private IWmsAsnService asnService;
    @Autowired
    private IWmsAsyncMsgService asyncMsgService;

    @Override
    public List<WmsASNReceiveVo> findReceiveListByParam(String searchParam) {
        return wmsPdaMapper.findReceiveListByParam(searchParam);
    }

    @Override
    public List<WmsASNReceiveVo> findReceiveDetailListByParam(String asnId, String searchParam) {
        return wmsPdaMapper.findReceiveDetailListByParam(asnId,searchParam);
    }

    @Override
    public Result<?> initReceiveDetailInputInfo(String detailId) {
        Result<JSONObject> result = new Result();
        WmsAsnDetail asnDetail = asnDetailService.getById(detailId);
        if(ObjectUtils.isEmpty(asnDetail)){
            return Result.error("未查询到相关收货明细!");
        }
        JSONObject obj = new JSONObject();
        //更新已收货数量百分比
        if(asnDetail.getReceivedQty().compareTo(asnDetail.getDemonQty()) < 0){
            String percentQty = asnDetail.getReceivedQty().stripTrailingZeros().toPlainString()+"/"+asnDetail.getDemonQty().stripTrailingZeros().toPlainString();
            obj.put("percentQty", percentQty);
        }
        result.setResult(obj);
        result.setSuccess(true);
        result.setCode(200);
        return result;
    }

    @Override
    public Result<?> receiveDetail(JSONObject jsonObject) {
        Result result = new Result();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String asnDetailId = jsonObject.getString("detailId");
            String receiveQuantity = jsonObject.getString("receiveQuantity");
            String palletNo = jsonObject.getString("palletNo");
            String boxNo = jsonObject.getString("boxNo");
            String lot01 = jsonObject.getString("lot01");
            String lot02 = jsonObject.getString("lot02");
            String lot03 = jsonObject.getString("lot03");
            String lot04 = jsonObject.getString("lot04");
            String lot05 = jsonObject.getString("lot05");
            String lot06 = jsonObject.getString("lot06");
            String lot07 = jsonObject.getString("lot07");
            if(StringUtils.isEmpty(receiveQuantity)){
                throw new WmsBusinessException("请输入收货数量!");
            }
            BigDecimal receiveQty = new BigDecimal(receiveQuantity);
            WmsAsnDetail asnDetail = asnDetailService.getById(asnDetailId);
            WmsAsn asn = asnService.getById(asnDetail.getAsnId());
            asnDetail.setPalletNo(palletNo);
            asnDetail.setBoxNo(boxNo);
            if(!StringUtils.isEmpty(lot01)){
                asnDetail.setLot01(format.parse(lot01));
            }
            asnDetail.setLot02(lot02);
            asnDetail.setLot03(lot03);
            asnDetail.setLot04(lot04);
            asnDetail.setLot05(lot05);
            asnDetail.setLot06(lot06);
            asnDetail.setLot07(lot07);
            asnDetail.setInputReceiveQty(receiveQty);
            asnDetailService.detailReceive(asnDetail);
            //PDA收获自动分配库位，自动分配货位
            JSONObject jsonMsg = new JSONObject();
            jsonMsg.put("businessCode", WmsMsgTypeEnums.MSG1.getCode());
            jsonMsg.put("relateBusinessId", asn.getId());
            asyncMsgService.genAsyncMsg(JSONObject.toJSONString(jsonMsg), WmsMsgTypeEnums.MSG1.getCode(), asn.getCode());
            //比较收货数量跳转相应页面
            LambdaQueryWrapper<WmsAsnDetail> asnDetailQuery = new LambdaQueryWrapper<>();
            asnDetailQuery.eq(WmsAsnDetail::getAsnId,asn.getId());
            List<WmsAsnDetail> details = asnDetailService.list(asnDetailQuery);
            BigDecimal totalReceivedQty = details.stream().map(WmsAsnDetail::getReceivedQty).reduce(BigDecimal.ZERO,BigDecimal::add);
            BigDecimal totalBaseQty = details.stream().map(WmsAsnDetail::getDemonQty).reduce(BigDecimal.ZERO,BigDecimal::add);
            //明细部分收货跳转当前页面继续收货
            if (asnDetail.getReceivedQty().compareTo(asnDetail.getDemonQty()) < 0) {
                result.setCode(1);
                result.setSuccess(Boolean.TRUE);
                result.setMessage("部分收货成功!");
            }
            if (asnDetail.getReceivedQty().compareTo(asnDetail.getDemonQty()) >= 0) {
                //全部收货完成跳转ASN主单页面
                if (totalReceivedQty.compareTo(totalBaseQty) >= 0) {
                    result.setCode(3);
                    result.setSuccess(Boolean.TRUE);
                    result.setMessage("整单收货成功!");
                } else {
                    result.setCode(2);
                    result.setSuccess(Boolean.TRUE);
                    result.setMessage("明细收货成功!");
                }
            }
            return result;
        }catch (Exception e){
            result.setSuccess(Boolean.FALSE);
            result.setMessage(e.getMessage());
            return result;
        }
    }

}
